<template>
  <div>
    <table class="description-wrap">
      <thead>
        <tr>
          <th class="description-head" :colspan="column">
            <p class="description-title">{{ header }}</p>
          </th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(row, rowIndex) in groupedRows" :key="rowIndex">
          <td v-for="(cell, cellIndex) in Object.values(row)" :key="cellIndex">
            <el-tooltip
              class="item"
              effect="dark"
              :content="`${cell[propsConfig.label]}: ${cell[propsConfig.value]}`"
              placement="top"
            >
              <p class="row-content">
                <span v-if="cell[propsConfig.label]" class="label"
                  >{{ cell[propsConfig.label] }}:
                </span>
                <span class="value">{{ cell[propsConfig.value] }}</span>
              </p>
            </el-tooltip>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script setup>
import { computed } from 'vue';
import { ElTooltip } from 'element-plus';
const props = defineProps({
  header: {
    type: String,
    required: true,
  },
  data: {
    type: Array,
    required: true,
  },
  column: {
    type: Number,
    default: 4,
  },
  // 选项配置
  propsConfig: {
    type: Object,
    default: () => ({
      label: 'parentName',
      value: 'craftsName',
    }),
  },
});

const groupedRows = computed(() => {
  const result = [];
  for (let i = 0; i < props.data.length; i += props.column) {
    const group = props.data.slice(i, i + props.column);
    while (group.length < props.column) {
      group.push({}); // 用空对象补全
    }
    result.push(group);
  }
  return result;
});
</script>

<style scoped lang="scss">
table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed; /* 每列宽度相等 */
}

td,
th {
  border: 1px solid #d9d9d9;
  line-height: 35px;
  text-align: left;
  overflow: hidden; /* 隐藏溢出的内容 */
  white-space: nowrap; /* 禁止换行 */
  text-overflow: ellipsis; /* 显示省略号 */
}

.description-wrap {
  margin-bottom: 16px;
}
.description-head {
  border: 1px solid #d9d9d9;
  border-width: 1px 1px 0;
  padding: 0 20px;
  border-bottom: none;
  height: 35px;
  line-height: 35px;
  background: #fafafa;
  text-align: left;
}
.description-title {
  color: #383838;
  font-size: 14px;
  font-weight: 400;
}

.row-content {
  padding: 0 20px;
  color: #383838;
  font-size: 14px;
  font-family: 'SourceHanSansCN';
  .label {
    padding-right: 4px;
    color: #666666;
  }
}
</style>
